﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace SplashFormV2
{
    public partial class SplashForm : Form
    {
        static void Main(string[] args)
        {

        }
        public SplashForm()
        {
            InitializeComponent();
        }
        private delegate void CloseDelegate();
        private static SplashForm splashForm;
        static public void ShowSplashScreen()
        {
            Thread _thread = new Thread(new ThreadStart(SplashForm.ShowForm));
            _thread.IsBackground = true;
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }
        static public void ShowSplashScreen(string caption, string message)
        {
            Thread _thread = new Thread(new ParameterizedThreadStart(SplashForm.ShowForm));
            _thread.IsBackground = true;
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start(new TipMessage() { Caption = caption, Message = message });
        }
        private static void ShowForm()
        {
            ShowForm(null);
        }
        private static void ShowForm(object tipMessage)
        {
            if (splashForm == null || splashForm.IsDisposed)
            {
                splashForm = new SplashForm();
                splashForm.TopMost = true;
                if (tipMessage != null)
                {
                    TipMessage _tipMessage = (TipMessage)tipMessage;
                    splashForm.lblCaption.Text = _tipMessage.Caption.Trim();
                    splashForm.lblMessage.Text = _tipMessage.Message.Trim();
                    splashForm.Refresh();
                }
                splashForm.StartPosition = FormStartPosition.CenterScreen;
                Application.Run(splashForm);
            }
        }
        public static void CloseForm()
        {
            splashForm.Invoke(new CloseDelegate(SplashForm.CloseFormInternal));
        }

        private static void CloseFormInternal()
        {
            if (splashForm != null && !splashForm.IsDisposed)
            {
                splashForm.Dispose();
            }
        }
    }
}
